﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SiemensOPC;
using Opc.Ua;
using Opc.Ua.Client;
using VswService_V7.Config;
using VswService_V7.DataBase.Tables;
using System.Data;
using VswService_V7.Utility;

namespace VswService_V7.OPC.DB
{
    public class SMPBladeAddressDbData : SubscriptionDbDataBase
    {
        public SMPBladeAddressDbData(SimaticOPCClient opc, NodeConfig nodes) : base(opc, nodes)
        {
            bladeFirst = new BladeInfo("1");
            bladeFirst.bladeOd = "355";
            bladeFirst.bladeWt = "3.1";
            bladeFirst.blade_location = bladeLocation;
            bladeSecond = new BladeInfo("2");
            bladeSecond.bladeOd = "355";
            bladeSecond.bladeWt = "3.1";
            bladeSecond.blade_location = bladeLocation;
        }

        BladeInfo bladeFirst;
        BladeInfo bladeSecond;
        string bladeLocation = "SMP";
        public override void CreateSubscription()
        {
            try
            {
                // AccStripInfoAddress : ask & rsp
                // AccStripInfoAskAddress : stripNo
                // AccStripInfoRspAddress : recipID yds ps1 ps2

                opcServer.QuickAddMonitorItem(opcNodes.SMPBladeAddress.GrpoupName,
                    opcNodes.SMPBladeAddress.PublishTime, opcNodes.SMPBladeAddress.SampleTime,
                    opcNodes.SMPBladeAddress.GetNodeAddress(), opcNodes.SMPBladeAddress.GetNodeDisplayName(),
                    SubscriptionHandler);

                opcServer.QuickAddMonitorItem(opcNodes.SMPBladeInfoAddress.GrpoupName,
                    opcNodes.SMPBladeInfoAddress.PublishTime, opcNodes.SMPBladeInfoAddress.SampleTime,
                    opcNodes.SMPBladeInfoAddress.GetNodeAddress(), opcNodes.SMPBladeInfoAddress.GetNodeDisplayName(),
                    SubscriptionHandlerData);
            }
            catch
            {
            }
        }
        private void SubscriptionHandler(MonitoredItem monitoredItem, MonitoredItemNotificationEventArgs e)
        {
            MonitoredItemNotification notification = e.NotificationValue as MonitoredItemNotification;
            if (notification == null)
                return;
            if (monitoredItem.DisplayName == "BladeUpdate_1")
            {
                bool ask = false;
                if (bool.TryParse(notification.Value.Value.ToString(), out ask))
                {
                    if (ask)
                    {

                        //***WHJ1112 
                        Console.WriteLine("blade  1  ask");
                        Task.Run(() => HandleBladeFirst());
                    }
                    else
                    {
                        opcServer.WriteValue("false", opcNodes.SMPBladeAddress.BladeRsp_1);
                    }
                }
            }

            if (monitoredItem.DisplayName == "BladeUpdate_2")
            {
                bool ask = false;
                if (bool.TryParse(notification.Value.Value.ToString(), out ask))
                {
                    if (ask)
                    {

                        //***WHJ1112 
                        Console.WriteLine("blade  2  ask");
                        Task.Run(() => HandleBladeSecond());
                    }
                    else
                    {
                        opcServer.WriteValue("false", opcNodes.SMPBladeAddress.BladeRsp_2);
                    }
                }
            }

        }

        private void SubscriptionHandlerData(MonitoredItem monitoredItem, MonitoredItemNotificationEventArgs e)
        {
            MonitoredItemNotification notification = e.NotificationValue as MonitoredItemNotification;
            if (notification == null || notification.Value.Value == null)
                return;

            switch (monitoredItem.DisplayName)
            {
                case "Bld1_SNr":
                    {
                        if (notification.Value.Value != null)
                        {
                            bladeFirst.bladeSn = notification.Value.Value.ToString();
                        }
                        break;
                    }


                case "Bld1_OD":
                    {
                        if (notification.Value.Value != null)
                        {
                            bladeFirst.bladeOd = Formats.ConvertUnit(notification.Value.Value.ToString(), VswUnit.Bld_OD_L2,1);
                        }
                        break;
                    }


                case "Bld1_WT":
                    {
                        if (notification.Value.Value != null)
                        {
                            bladeFirst.bladeWt = Formats.ConvertUnit(notification.Value.Value.ToString(), VswUnit.Bld_WT_L2,1);
                        }
                        break;
                    }

                case "Bld1_TNr":
                    {
                        if (notification.Value.Value != null)
                        {
                            bladeFirst.bladeTn = notification.Value.Value.ToString();
                        }
                        break;
                    }

                case "Bld1_CutArea":
                    {
                        if (notification.Value.Value != null)
                        {
                            bladeFirst.blade_area = Formats.ConvertUnit(notification.Value.Value.ToString(), VswUnit.Bld_CutArea_L2,1);
                        }
                        break;
                    }

                case "Bld2_SNr":
                    {
                        if (notification.Value.Value != null)
                        {
                            bladeSecond.bladeSn = notification.Value.Value.ToString();
                        }
                        break;
                    }


                case "Bld2_OD":
                    {
                        if (notification.Value.Value != null)
                        {
                            bladeSecond.bladeOd = Formats.ConvertUnit(notification.Value.Value.ToString(), VswUnit.Bld_OD_L2,1);
                        }
                        break;
                    }


                case "Bld2_WT":
                    {
                        if (notification.Value.Value != null)
                        {
                            bladeSecond.bladeWt = Formats.ConvertUnit(notification.Value.Value.ToString(), VswUnit.Bld_WT_L2,1);
                        }
                        break;
                    }

                case "Bld2_TNr":
                    {
                        if (notification.Value.Value != null)
                        {
                            bladeSecond.bladeTn = notification.Value.Value.ToString();
                        }
                        break;
                    }

                case "Bld2_CutArea":
                    {
                        if (notification.Value.Value != null)
                        {
                            bladeSecond.blade_area = Formats.ConvertUnit(notification.Value.Value.ToString(), VswUnit.Bld_CutArea_L2,1);
                        }
                        break;
                    }
                default:
                    break;


            }
        }



        private async Task HandleBladeFirst()
        {
            await Task.Delay(2000);
            bladeFirst.updateTime = DateTime.Now.ToString(Formats.DataFormat);
            try
            {
                BLADE_INFO.InsertBladeRecord(bladeFirst);
            }
            catch(Exception ex)
            {
                LogHelper.WriteLog("HandleBladeFirst", ex);
            }
            
            opcServer.WriteValue("true", opcNodes.SMPBladeAddress.BladeRsp_1);
        }


        private async Task HandleBladeSecond()
        {
            await Task.Delay(2000);
            bladeSecond.updateTime = DateTime.Now.ToString(Formats.DataFormat);
            try
            {
                BLADE_INFO.InsertBladeRecord(bladeSecond);
            }
            catch(Exception ex)
            {
                LogHelper.WriteLog("HandleBladeSecond", ex);
            }
            
            opcServer.WriteValue("true", opcNodes.SMPBladeAddress.BladeRsp_2);
        }
    }
}
